﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_SanPham_Product_Info : System.Web.UI.UserControl
{    
    TblSanPhamManager _TblSanPhamManager = new TblSanPhamManager();
    public int NewsID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { NewsID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { NewsID = 0; }

            loadCateParent();

            FunctionExtend.LoadTGBaoHanh(ddlDVBaoHanh);

            LoadTienTeToDropdown();

            //LoadPhuKien(NewsID);

            if (NewsID == 0) //Add cate
            {
                lblTitle.Text = "Thêm tin";
                imgbtn_Delete.Enabled = false;
                img_news.Visible = imgbtn_Delete.Visible = false;
                Toolbox1.ShowDel = Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
                //hide tab
                //tabitem2.Visible =tabitem4.Visible= false;
            }
            else //sua cate
            {
                imgbtn_Delete.Enabled = true;
                lblDelete.Visible = true;
                imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
                lblTitle.Text = "Sửa tin: " + NewsID;
                BindData(NewsID);
            }
        }
    }

    private void LoadTienTeToDropdown()
    {
        TblTienTeEntity _TblTienTeEntity = new TblTienTeEntity();
        _TblTienTeEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblTienTeEntity.SortDirection = "ASC";
        DataTable _dt = (new TblTienTeManager()).be_selectToGridView(_TblTienTeEntity);
        if (_dt != null && _dt.Rows.Count > 0)
        {
            //HNG.LIB.BIND_SOURCE_V3.Bind_DropDownList(ddlTienTe, _dt, TblTienTeFields.Code.Name, TblTienTeFields.Mota.Name);
            ListItem ItemF = new ListItem("--", "0");
            ddlTienTe.Items.Add(ItemF);
            foreach (DataRow r in _dt.Rows)
            {
                ListItem Items = new ListItem(r["Mota"].ToString(), r["Code"].ToString());
                ddlTienTe.Items.Add(Items);
            }
            ddlTienTe.SelectedIndex = 0;
        }
    }


    //private void LoadPhuKien(int SanPhamID)
    //{
    //    TblSanPhamPhuKienEntity _TblSanPhamPhuKienEntity = new TblSanPhamPhuKienEntity();
    //    _TblSanPhamPhuKienEntity.SanPhamId = SanPhamID;
    //    DataTable _dtPhuKien = new TblSanPhamPhuKienManager().be_SelectToGridview(_TblSanPhamPhuKienEntity);
    //    if (_dtPhuKien != null)
    //    {
    //        ListPhuKien.DataTextField = TblSanPhamPhuKienFields.TenPhuKien.Name;
    //        ListPhuKien.DataValueField = TblSanPhamPhuKienFields.Id.Name;
    //        ListPhuKien.DataSource = _dtPhuKien;
    //        ListPhuKien.DataBind();
    //    }
    //}

    //load chi tiet san pham
    protected void BindData(int ID)
    {
        
        TblSanPhamEntity _TblSanPhamEntity = _TblSanPhamManager.SelectOne(ID);
        if (_TblSanPhamEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        txtID.Text = ID.ToString();
        hidID.Value = ID.ToString();
        txtTieude.Text = Server.HtmlDecode(_TblSanPhamEntity.TenSanPham);
        txtTomTat.Text = Server.HtmlDecode(_TblSanPhamEntity.TomTat);
        fckMoTa.Value = _TblSanPhamEntity.Mota;
        txtPriceCurrent.Text = _TblSanPhamEntity.GiaBan.ToString();
        txtPriceOld.Text = _TblSanPhamEntity.GiaCu.ToString();
        txtTomtatHome.Text = Server.HtmlDecode(_TblSanPhamEntity.TomTatHome);
        txtTGbaoHanh.Text = _TblSanPhamEntity.ThoiGianBaoHanh.ToString();
        txtBarcode.Text = _TblSanPhamEntity.Barcode;
        //fckTongquan.Value = _TblSanPhamEntity.Tongquan;
        //fckTinhNang.Value = _TblSanPhamEntity.Tinhnang;
        //txtThuTu.Text = _TblSanPhamEntity.ThuTu.ToString();
        //fckBaogom.Value = _TblSanPhamEntity.Baogom;
        //fckHinhthuc.Value = _TblSanPhamEntity.Hinhthuc;
        //fckBaohanh.Value = _TblSanPhamEntity.Baohanh;
        string dvbh = _TblSanPhamEntity.DonViBaoHanh;
        ddlDVBaoHanh.SelectedValue = dvbh;
        ddlTienTe.SelectedValue = _TblSanPhamEntity.MaTienTe;
        txtSoLuong.Text = _TblSanPhamEntity.SoLuong.ToString();
        txtMetakeyword.Text = Server.HtmlDecode(_TblSanPhamEntity.MetaKeyword);
        txtMetaDescription.Text = Server.HtmlDecode(_TblSanPhamEntity.MetaDesc);
        string DMnews = _TblSanPhamEntity.DanhMucSpid.ToString();
        ddlCategoryParent.SelectedValue = DMnews;
        img_news.Visible = true;
        img_news.ImageUrl = (_TblSanPhamEntity.Image != "") ? Server.HtmlDecode(_TblSanPhamEntity.Image) : "~/AdminPortal/Images/icon/no_image.jpg";
        if (string.IsNullOrEmpty(_TblSanPhamEntity.Image))
        {
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_hiden.gif";
            img_news.Visible = false;
        }
        else
        {
            img_news.Visible = true;
        }
        chkNoiBat.Checked = Convert.ToBoolean(_TblSanPhamEntity.SpnoiBat);
        chkHot.Checked = Convert.ToBoolean(_TblSanPhamEntity.SpHot);
        chkStatus.Checked = Convert.ToBoolean(_TblSanPhamEntity.TrangThai);
    }

    private void loadCateParent()
    {
        TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
        _TblDanhMucSanPhamEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        DataTable _dtDM = new TblDanhMucSanPhamManager().be_SelectToGrivew(_TblDanhMucSanPhamEntity);
        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE_V3.Bind_DropDownList(ddlCategoryParent, _dtDM, TblDanhMucSanPhamFields.TenDanhMuc.Name, TblDanhMucSanPhamFields.Id.Name, TblDanhMucSanPhamFields.DanhMucCha.Name, true);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblSanPhamEntity _TblSanPhamEntity = _TblSanPhamManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblSanPhamEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblSanPhamEntity.Image))
                {
                    string ImageDefault = Server.MapPath(_TblSanPhamEntity.Image);
                    try { File.Delete(ImageDefault); }
                    catch { }
                }
            }
            _TblSanPhamManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtTieude.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tiêu đề tin !</span>"; txtTieude.Focus(); return; }
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/ProductEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        TblSanPhamEntity _TblSanPhamEntity = new TblSanPhamEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblSanPhamEntity.Id = Convert.ToInt32(hidID.Value);
        else
        {
            _TblSanPhamEntity.NgayDang = DateTime.Now;
        }
        //_TblSanPhamEntity.TruyCap = 1;
        _TblSanPhamEntity.DanhMucSpid = Convert.ToInt32(ddlCategoryParent.SelectedValue);
        _TblSanPhamEntity.LangId = "vn";
        _TblSanPhamEntity.TenSanPham = Server.HtmlEncode(txtTieude.Text);
        _TblSanPhamEntity.TomTat = Server.HtmlEncode(txtTomTat.Text);
        _TblSanPhamEntity.Mota = fckMoTa.Value;
        _TblSanPhamEntity.Barcode = txtBarcode.Text;
        //_TblSanPhamEntity.Tongquan = fckTongquan.Value;
        //_TblSanPhamEntity.Tinhnang = fckTinhNang.Value;
        //_TblSanPhamEntity.Baogom = fckBaogom.Value;
        //_TblSanPhamEntity.Hinhthuc = fckHinhthuc.Value;
        //_TblSanPhamEntity.Baohanh = fckBaohanh.Value;
        if (!string.IsNullOrEmpty(txtThuTu.Text))
            _TblSanPhamEntity.ThuTu = Convert.ToInt32(txtThuTu.Text);
        _TblSanPhamEntity.TomTatHome = Server.HtmlEncode(txtTomtatHome.Text);
        _TblSanPhamEntity.TenDanhMuc = ddlCategoryParent.SelectedItem.Text;
        _TblSanPhamEntity.MaTienTe = ddlTienTe.SelectedValue;
        if (!string.IsNullOrEmpty(txtPriceCurrent.Text))
            _TblSanPhamEntity.GiaBan = Convert.ToDouble(txtPriceCurrent.Text);
        if (!string.IsNullOrEmpty(txtPriceOld.Text))
            _TblSanPhamEntity.GiaCu = Convert.ToDouble(txtPriceOld.Text);
        if (!string.IsNullOrEmpty(txtTGbaoHanh.Text))
            _TblSanPhamEntity.ThoiGianBaoHanh = Convert.ToInt32(txtTGbaoHanh.Text);
        _TblSanPhamEntity.DonViBaoHanh = ddlDVBaoHanh.SelectedValue;
        if (!string.IsNullOrEmpty(txtSoLuong.Text))
            _TblSanPhamEntity.SoLuong = Convert.ToDouble(txtSoLuong.Text);
        _TblSanPhamEntity.MetaKeyword = Server.HtmlEncode(txtMetakeyword.Text);
        _TblSanPhamEntity.MetaDesc = Server.HtmlEncode(txtMetaDescription.Text);
        if (FileUpload1.FilePath != "")
        {
            string ImageDefault = Server.MapPath(_TblSanPhamEntity.Image);
            try { File.Delete(ImageDefault); }
            catch { }
            _TblSanPhamEntity.Image = Server.HtmlEncode(FileUpload1.FilePath);
        }
        _TblSanPhamEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblSanPhamEntity.SpnoiBat = (chkNoiBat.Checked == true) ? true : false;
        _TblSanPhamEntity.SpHot = (chkHot.Checked == true) ? true : false;
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblSanPhamManager.Update(_TblSanPhamEntity);
            //luu Images
            Savemages();
            ////luu san pham mua kem
            //SaveSanPhamMuaKem();
        }
        else
        {
            _TblSanPhamManager.Insert(_TblSanPhamEntity);
            hidID.Value = _TblSanPhamEntity.Id.ToString();
        }

    }

    //private void SaveSanPhamMuaKem()
    //{
    //    string strPID = inc_SanPhamLienQuan1.ListProductID;
    //    try
    //    {
    //        new TblSanPhamKhuyenMaiManager().DeleteByIdsanPham(Convert.ToInt32(hidID.Value));
    //        if (!string.IsNullOrEmpty(strPID))
    //        {
    //            string[] ListPID = strPID.Split(',');
    //            foreach (string s in ListPID)
    //            {
    //                if (s != "")
    //                {
    //                    TblSanPhamKhuyenMaiEntity _TblSanPhamKhuyenMaiEntity = new TblSanPhamKhuyenMaiEntity();
    //                    _TblSanPhamKhuyenMaiEntity.IdsanPham = Convert.ToInt32(hidID.Value);
    //                    _TblSanPhamKhuyenMaiEntity.IdsanPhamKhuyenMai = Convert.ToInt32(s);

    //                    new TblSanPhamKhuyenMaiManager().Insert(_TblSanPhamKhuyenMaiEntity);
    //                }
    //            }
    //        }
    //    }
    //    catch { }
    //}

    private void Savemages() {
        try
        {
            //hien thi nhieu anh cho san pham nay
            if (!string.IsNullOrEmpty(FileUploadMulti1.linkfile))
            {
                string[] ListImg = FileUploadMulti1.linkfile.Split(',');
                foreach (string s in ListImg)
                {
                    if (s != "")
                    {
                        TblHinhAnhSanPhamEntity _TblHinhAnhSanPhamEntity = new TblHinhAnhSanPhamEntity();
                        _TblHinhAnhSanPhamEntity.IdsanPham = Convert.ToInt32(hidID.Value);
                        _TblHinhAnhSanPhamEntity.Image = s;
                        _TblHinhAnhSanPhamEntity.TrangThai = true;
                        new TblHinhAnhSanPhamManager().Insert(_TblHinhAnhSanPhamEntity);
                    }
                }
                
            }
        }
        catch { }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblSanPhamEntity _TblSanPhamEntity = _TblSanPhamManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblSanPhamEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblSanPhamEntity.Image))
                {
                    string defaultImage = Server.MapPath(_TblSanPhamEntity.Image);
                    try { File.Delete(defaultImage); }
                    catch { }
                    _TblSanPhamEntity.Image = "";
                    _TblSanPhamManager.Update(_TblSanPhamEntity);
                }
            }
        }
        BindData(Convert.ToInt32(hidID.Value));
    }
}